<?php

/**
 * User: thanhdx
 * Date: 4/5/12
 * Time: 5:41 PM
 */
class UserController extends Controller {

    public $layout = '//layouts/column2';

    public function actionAddRole() {
        $model = new AddRoleForm();
        if (isset($_POST['AddRoleForm'])) {
            $model->attributes = $_POST['AddRoleForm'];
            if ($model->validate()) {
                
            }
        }
        $this->render('add_role', array('model' => $model));
    }

    public function actionIndex() {
        $model = new User('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['User']))
            $model->attributes = $_GET['User'];

        $this->render('index', array(
            'model' => $model,
        ));
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate() {
        $model = new User;

        $this->performAjaxValidation($model);

        if (isset($_POST['User'])) {
            $_POST['User']['password'] = $model->hashPassword(trim($_POST['User']['password']));
            $model->attributes = $_POST['User'];
            if ($model->save())
                $this->redirect(array('view', 'id' => $model->id));
        }
        
        $this->render('create', array(
            'model' => $model,
            'roles' => Rights::getAuthItemSelectOptions(2),
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id) {

        $model = new UpdateUserForm($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['UpdateUserForm'])) {
            $model->attributes = $_POST['UpdateUserForm'];
            /* $data = $_POST['User'];
              $model->display_name = $data['display_name'];
              $model->role = $data['role'];
              $model->home_page = $data['home_page'];
              $model->email = $data['email']; */
            //var_dump($model->attributes);die;
            if ($model->validate() && $model->save()) {
                $this->redirect(array('index'));
            } else {
                //var_dump($model->errors);die;
            }
        }

        

        $this->render('update', array(
            'model' => $model,
            //'roles' => $this->getRoleOptions(),
            'roles' => Rights::getAuthItemSelectOptions(2),
        ));
    }


    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request
            $this->loadModel($id)->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     * @return \CActiveRecord
     */
    public function loadModel($id) {
        $model = User::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'user-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
